#include "../../../libraries/sockets/udp_socket.h"
#include <iostream>
#include <fstream>
#include <string>
#include "../../../config.h"

#include <cstdlib>



using namespace std;

int main(int argc, char *argv[])

{

    enum oper{Sm, Rs, Dv, Mp, Ex,Rz };
    if (argc<3)
    {
        cout <<"use this format: ./server IP portno" <<endl;
        exit(1);
    }

    UDP_socket clientUDP;

    clientUDP.connect(argv[1]);

    if (clientUDP.getServer()==0) clientUDP.error("Unknown host");

    clientUDP.setPortno(atoi(argv[2]));


    string op, num1,num2, message1, message2, resultado, fin;

    do{
        cout << "[Sm]Suma, [Rs]Resta, [Dv]División, [Mp]Multiplicación, [Ex]Exponente, [Rz]Raiz" << endl;
        cin >> op;

        char ope[2];
        if (op=="Sm" ) sprintf(ope,"%d", Sm);
        else if (op=="Rs" ) sprintf(ope,"%d", Rs);
        else if (op=="Dv" ) sprintf(ope,"%d", Dv);
        else if (op=="Mp" ) sprintf(ope,"%d", Mp);
        else if (op=="Ex" ) sprintf(ope,"%d", Ex);
        else if (op=="Rz" ) sprintf(ope,"%d", Rz);
	else {
		sprintf(ope,"%d", -1); //-1 if not known
                cout << "Operación no reconocida\n" << endl;
		exit(-1);
	}

        clientUDP << ope;

        clientUDP >> message1;
        cout << message1;

        cin >> num1;
        clientUDP << num1;



        if(op!="Rz")
        {
            clientUDP >> message2;
            cout << message2;
            cin >> num2;
            clientUDP << num2;
        }

        clientUDP >> resultado;
        cout << "Resultado: " << resultado << endl;

        cout << endl << "¿Desea realizar otra operación? [Y]Yes, [N]No " << endl;
        cin >> fin;
        clientUDP <<fin;
    }while(fin=="Y");

    return 0;
}
